package com.baozun.houji.goods.activity

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.KeyEvent
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.baozhun.mall.common.arouter.RoutePath
import com.baozhun.mall.common.base.BaseActivity
import com.baozhun.mall.common.ext.hideSoftKeyboard
import com.baozhun.mall.common.model.enums.GoodsSource
import com.baozhun.mall.common.util.CacheUtil
import com.baozhun.mall.common.util.Constants
import com.baozun.houji.goods.R
import com.baozun.houji.goods.databinding.ActivitySearchResultBinding
import com.baozun.houji.goods.fragment.SearchResultFragment
import com.baozun.houji.goods.viewmodel.state.SearchResultViewModel
import com.hjq.bar.OnTitleBarListener
import com.orhanobut.logger.Logger


@Route(path = RoutePath.Home.SEARCH_RESULT)
class SearchResultActivity : BaseActivity<SearchResultViewModel, ActivitySearchResultBinding>(), OnTitleBarListener,
    View.OnKeyListener,
    ViewPager.OnPageChangeListener, TextWatcher {

    private val initKeywords by lazy { intent.getStringExtra(Constants.Common.BUNDLE_NAME).toString() }

    /**
     * 搜索类型tab
     */
    private var mSearchTab = arrayOf(
        GoodsSource.TAO_BAO_AND_T_MALL.desc,
        GoodsSource.JD.desc,
        GoodsSource.PDD.desc,
//        GoodsSource.SELF.desc,
    )

    /**
     * 搜索类型fragment
     */
    private val mTabFragmentList = arrayListOf<Fragment>()

    override fun initView(savedInstanceState: Bundle?) {
        mDataBinding.searchEt.setText(initKeywords)
        mDataBinding.searchEt.setOnKeyListener(this)
        mDataBinding.searchEt.addTextChangedListener(this)

        initTabView()
    }

    /**
     * 初始化顶部tabView
     */
    private fun initTabView() {
        mTabFragmentList.clear()
//        mTabFragmentList.add(SearchResultFragment.newInstance(GoodsSource.TAO_BAO_AND_T_MALL, initKeywords))
//        mTabFragmentList.add(SearchResultFragment.newInstance(GoodsSource.JD, initKeywords))
//        mTabFragmentList.add(SearchResultFragment.newInstance(GoodsSource.PDD, initKeywords))
        mTabFragmentList.add(SearchResultFragment.newInstance(GoodsSource.SELF, initKeywords))
        mDataBinding.viewpagerGoods.offscreenPageLimit = 1
        mDataBinding.tabLayoutGoodsType.setViewPager(
            mDataBinding.viewpagerGoods,
            mSearchTab,
            this,
            mTabFragmentList
        )
        mDataBinding.viewpagerGoods.addOnPageChangeListener(this)
        //修复框架默认选中不加粗bug
        mDataBinding.tabLayoutGoodsType.onPageSelected(0)
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v?.id) {
            R.id.ic_back -> finish()

        }
    }

    override fun onKey(view: View?, keyCode: Int, event: KeyEvent?): Boolean {
        return if (keyCode == KeyEvent.KEYCODE_ENTER && event?.action == KeyEvent.ACTION_UP) {
            (mTabFragmentList[mDataBinding.viewpagerGoods.currentItem] as SearchResultFragment).search(true)
            hideSoftKeyboard(mDataBinding.searchEt)
            true
        } else {
            false
        }
    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
    }

    override fun onPageSelected(position: Int) {
        Logger.e("onPageSelected-->${position}")

        if (position === mSearchTab.indexOf(GoodsSource.SELF.desc) && !CacheUtil.isLogin()) {
            mDataBinding.viewpagerGoods.currentItem = 0
            ARouter.getInstance().build(RoutePath.Me.LOGIN).navigation()
        } else {
            mDataBinding.tabLayoutGoodsType.textBold = 1
            hideSoftKeyboard(mDataBinding.searchEt)
            (mTabFragmentList[position] as SearchResultFragment).search(true)
        }

    }

    override fun onPageScrollStateChanged(state: Int) {
    }

    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

    }

    override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
    }

    override fun afterTextChanged(p0: Editable?) {
        mTabFragmentList.forEach {
            (it as SearchResultFragment).updateKeyWord(p0.toString())
        }
    }

}